package edu.netjava.dbstructor;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BeanBaseConnector {
	
	
	public static Connection conn;
	public final static String DATA_BASE = "student";
	public final static String TABLE = "info";
	public static String SQL = "SELECT id,name,AGE,class FROM "+ TABLE ;
	List<StudentBean> list;
	
	
	
	public List<StudentBean> getAllStudentInfo() throws SQLException{
		
		conn = Connector.getConnection();
		
		java.sql.PreparedStatement pa = conn.prepareStatement(SQL);
		
		ResultSet rs = pa.executeQuery();
		
		list = new ArrayList<StudentBean>();
		
		while(rs.next()){
			
			StudentBean sb = new StudentBean();
			
			sb.setId(rs.getInt(1));
			sb.setName(rs.getString(2));
			sb.setAge(rs.getInt(3));
			sb.setClas(rs.getString(4));
			
			list.add(sb);
		
			
		}
		
		
		return list;
		
	}
	
	
	public static void main(String args[]){
		
		BeanBaseConnector bc = new  BeanBaseConnector();
		
		try {
			
			List<StudentBean> lis = bc.getAllStudentInfo();
			
			for(StudentBean sb:lis){
				
				System.out.print(sb.getId()+"\t"+sb.getName()
						+"\t"+sb.getAge()+"\t" + sb.getClas() + "\n");
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
	}
	
	
	
	
	
	

}
